home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / pleas / ole / visio / network / main.bas < prev    next >
Encoding:
BASIC Source File  |  1994-05-18  |  17.0 KB  |  586 lines

  1. '------------------------------------------------------------------------------
  2. '------------------------------------------------------------------------------
  3. '--
  4. '--                        Network Diagramming Example
  5. '--                       (C)1993 Shapeware Corporation
  6. '--
  7. '--   File Name : main.bas
  8. '--
  9. '-- Description :
  10. '--
  11. '------------------------------------------------------------------------------
  12. '------------------------------------------------------------------------------
  13.  
  14. 'This file contains sample code for using Visual Basic and OLE 2.0 to
  15. 'automatically create a Visio network diagram from a Microsoft Access
  16. 'database.
  17. '
  18. 'IMPORTANT:  NETVB.ZIP is ONLY a sample, not a released product.  It was
  19. 'not extensively tested, and has no guarantee.  In addition, we do not provide
  20. 'documentation or support for this file.
  21. '
  22. 'After you download and unzip the file, read the file "abstract.wri" to get
  23. 'more information about what you need before running the file.
  24. '
  25. 'To run the file*, open your Windows File Manager, go to the directory where
  26. 'you placed the unzipped files, and double-click the file "netdiag.mak."  This
  27. 'will open Visual Basic.  Press F5 to run the program.  The program will run,
  28. 'and from its File menu, choose Open Database.  Then choose "network.mdb"
  29. 'which is included in NETVB.ZIP and watch your blank Visio drawing page
  30. 'turn into a basic network diagram!
  31. '
  32. '*It doesn't matter if you already have Visio running.  If it is, that instance
  33. 'will be used.  If not, the program will start it.
  34. '
  35.  
  36. Option Explicit
  37. Option Base 0
  38.  
  39. '--
  40. '-- Win 3.1 API Helpers
  41. '--
  42.  
  43. Global Const OFN_HIDEREADONLY = &H4&
  44. Global Const OFN_OVERWRITEPROMPT = &H2&
  45.  
  46. Global Const IDYES = 6
  47. Global Const IDNO = 7
  48.  
  49. Global Const MB_YESNO = 4
  50. Global Const MB_ICONQUESTION = 32
  51. Global Const MB_ICONEXCLAMATION = 48
  52. Global Const MB_ICONINFORMATION = 64
  53.  
  54. '--
  55. '-- Network diagram constants
  56. '--
  57.  
  58. Global Const NDB_NET_TABLE_NAME = "NETWORK"
  59. Global Const NDB_NET_TABLE_INDEX = "NET_INFO_IDX"
  60.  
  61. Global Const NDB_NET_TABLE_NODE = "Node Type"
  62. Global Const NDB_NET_TABLE_TEXT = "Text"
  63. Global Const NDB_NET_TABLE_DATA1 = "Data1"
  64. Global Const NDB_NET_TABLE_DATA2 = "Data2"
  65.  
  66. Global Const NDB_PAGE_WIDTH = 3
  67. Global Const NDB_PAGE_HEIGHT = 5
  68.  
  69. '-- Network Stencil Constants
  70.  
  71. Global Const NDB_NET_TEMPLATE = "network.vst"
  72. Global Const NDB_NET_STENCIL = "network.vss"
  73.  
  74. Global Const NDB_NET_ETHERNET = "Ethernet"
  75. Global Const NDB_NET_BUS = "Bus"
  76. Global Const NDB_NET_STAR = "Star"
  77. Global Const NDB_NET_TOKEN_RING = "Token-ring"
  78. Global Const NDB_NET_FDDI_RING = "FDDI ring"
  79.  
  80. Global Const NDB_NODE_MAC = "Macintosh"
  81. Global Const NDB_NODE_PC = "Desktop PC"
  82. Global Const NDB_NODE_SERVER = "Server"
  83. Global Const NDB_NODE_WORKSTATION = "Workstation"
  84. Global Const NDB_NODE_TOWER = "Server / tower"
  85. Global Const NDB_NODE_PRINTER = "Printer"
  86.  
  87. '--
  88. '-- Module variables used by CreateDiagram and it's support functions.
  89. '--
  90.  
  91. Dim m_tblNetInfo As Table
  92.  
  93. Dim m_pag As Object
  94. Dim m_masts As Object
  95.  
  96.  
  97. '--
  98. '--   To place nodes on the diagram effectively we need to know a little about
  99. '-- them first.  This structure keeps the shapes unique NameID, pin and
  100. '-- network connection point.
  101. '--
  102.  
  103. Type NodeInfo
  104.     ' Name of Master (See MatchMasterToType)
  105.     '
  106.     strMaster As String
  107.  
  108.     ' Used when dropping shape on page.
  109.     '
  110.     iPinX As Double
  111.     iPinY As Double
  112.  
  113.     ' Side of shape that should be glued to.   See SIDE contstants in
  114.     ' VISSHEET.BAS
  115.     '
  116.     iSide As Integer
  117. End Type
  118.  
  119. Sub BuildNodeList (Nodes() As NodeInfo)
  120. '----------------------------------------
  121. '--- BuildNodeList ----------------------
  122. '--
  123. '--   Constructs a node list depending the contents of the network info table.
  124. '--
  125.     
  126.     Dim iNode As Integer, iNodes As Integer
  127.     Dim iPinX, iPinY
  128.  
  129.     iNode = 0: iNodes = 0
  130.     
  131.     ' First we loop through the table and count the number of records.
  132.     '
  133.     m_tblNetInfo.MoveFirst
  134.  
  135.     Do While Not m_tblNetInfo.EOF
  136.         iNodes = iNodes + 1
  137.         m_tblNetInfo.MoveNext
  138.     Loop
  139.  
  140.     ' Next we check the size of the table and, if no records exists, exit.
  141.     ' Otherwise we allocate the array.
  142.     '
  143.     If iNodes = 0 Then Exit Sub
  144.  
  145.     ReDim Nodes(0 To Min(iNodes, 8) - 1)
  146.  
  147.     ' Loop through the database and fill out each node structure.
  148.     '
  149.     m_tblNetInfo.MoveFirst
  150.  
  151.     iPinX = .5
  152.     iPinY = .5
  153.  
  154.     While Not m_tblNetInfo.EOF And (iNode < (UBound(Nodes) + 1))
  155.         ' Each node must have a unique master type assigned, it's pin computed
  156.         ' dependent upon which column it resides in and it's proper
  157.         ' connection point side determined.  The connect side is used with
  158.         ' BestExportPoint at draw time to glue to the network bus.
  159.         '
  160.         Nodes(iNode).strMaster = MatchMasterToType(m_tblNetInfo.Fields(0))
  161.  
  162.         Nodes(iNode).iPinX = iPinX
  163.         Nodes(iNode).iPinY = iPinY
  164.  
  165.         If iNode < 4 Then
  166.             Nodes(iNode).iSide = SIDE_RIGHT
  167.         Else
  168.             Nodes(iNode).iSide = SIDE_LEFT
  169.         End If
  170.  
  171.         If iNode = 3 Then
  172.             iPinX = NDB_PAGE_WIDTH - .5
  173.             iPinY = .5
  174.         Else
  175.             iPinY = iPinY + 1.25
  176.         End If
  177.  
  178.         iNode = iNode + 1
  179.         m_tblNetInfo.MoveNext
  180.     Wend
  181. End Sub
  182.  
  183. Sub CreateBlankDatabase (strFileName As String)
  184. '----------------------------------------
  185. '--- CreateBlankDatabase ----------------
  186. '--
  187. '--   Builds a blank Access database for the user.  Assumes the file name passed
  188. '-- does not exists.
  189. '--
  190.  
  191.     On Error GoTo lblNewDBaseErr
  192.  
  193.     Dim fldNode As New Field, fldText As New Field
  194.     Dim fldData1 As New Field, fldData2 As New Field
  195.     Dim dbDBase As Database, tblNetInfo As New TableDef, tblIndex As New Index
  196.     
  197.     ' First we create the database
  198.     '
  199.     Set dbDBase = CreateDatabase(strFileName, DB_LANG_GENERAL, DB_VERSION10)
  200.     
  201.     ' Initialize the table index to the node field.
  202.     '
  203.     tblIndex.Name = NDB_NET_TABLE_INDEX
  204.     tblIndex.Unique = False
  205.     tblIndex.Primary = True
  206.     tblIndex.Fields = NDB_NET_TABLE_NODE
  207.  
  208.     ' Set the table name.
  209.     '
  210.     tblNetInfo.Name = NDB_NET_TABLE_NAME
  211.     
  212.     ' Initialize the fields.
  213.     '
  214.     fldNode.Name = NDB_NET_TABLE_NODE
  215.     fldNode.Type = DB_TEXT
  216.     fldNode.Size = 255
  217.     tblNetInfo.Fields.Append fldNode
  218.  
  219.     fldText.Name = NDB_NET_TABLE_TEXT
  220.     fldText.Type = DB_TEXT
  221.     fldText.Size = 255
  222.     tblNetInfo.Fields.Append fldText
  223.     
  224.     fldData1.Name = NDB_NET_TABLE_DATA1
  225.     fldData1.Type = DB_TEXT
  226.     fldData1.Size = 255
  227.     tblNetInfo.Fields.Append fldData1
  228.  
  229.     fldData2.Name = NDB_NET_TABLE_DATA2
  230.     fldData2.Type = DB_TEXT
  231.     fldData2.Size = 255
  232.     tblNetInfo.Fields.Append fldData2
  233.  
  234.     ' Add the index to the table and table to the database.
  235.     '
  236.     'tblNetInfo.Indexes.Append tblIndex
  237.     dbDBase.TableDefs.Append tblNetInfo
  238.  
  239.     ' Clean Up.
  240.     '
  241.     dbDBase.Close
  242.     Exit Sub
  243.  
  244. lblNewDBaseErr:
  245.     MsgBox "Error creating blank database." & Chr(13) & Chr(10) & Error
  246.     Exit Sub
  247.  
  248.     Resume Next
  249. End Sub
  250.  
  251. Sub CreateDiagram (strFileName As String)
  252. '----------------------------------------
  253. '--- CreateDiagram ----------------------
  254. '--
  255. '--   Creates a network diagram from a database table.
  256. '--
  257. '-- Parameters : strFileName - Database file name
  258. '--
  259.     
  260.     On Error GoTo lblCreateDiagramErr
  261.  
  262.     Dim strMsg As String
  263.     Dim dbDBase As Database
  264.     Dim pag As Object, shp As Object, mast As Object
  265.     Dim NodeList() As NodeInfo
  266.  
  267.     ' First we try and initialize Visio
  268.     
  269.     If Not InitDiagram() Then Exit Sub
  270.  
  271.     ' Next we try and open the database.  Access should convert regardless of
  272.     ' format.
  273.     
  274.     Set dbDBase = OpenDatabase(strFileName)
  275.     
  276.     ' No error occurred so we try and open our table.  We use a constant
  277.     ' name for a database at this point but should prompt the user in the
  278.     ' future for what table they want to use.
  279.     
  280.     Set m_tblNetInfo = dbDBase.OpenTable(NDB_NET_TABLE_NAME)
  281.  
  282.     ' Next we build the node list for drawing.
  283.     
  284.     BuildNodeList NodeList()
  285.  
  286.     ' Finally we are ready to build the diagram.
  287.     
  288.     m_tblNetInfo.MoveFirst
  289.  
  290.     If Not m_tblNetInfo.EOF Then
  291.         DrawDiagram NodeList()
  292.     Else
  293.         MsgBox "Table is empty", MB_ICONINFORMATION, "Create Diagram"
  294.     End If
  295.  
  296.     ' Cleanup by closing table and database and releasing their resources.
  297.     
  298.     m_tblNetInfo.Close
  299.     dbDBase.Close
  300.  
  301.     Set m_tblNetInfo = Nothing
  302.     
  303.     MsgBox "Finished creating diagram.", MB_ICONINFORMATION, ""
  304.     Exit Sub
  305.  
  306. lblCreateDiagramErr:
  307.     strMsg = "Error creating network diagram." & Chr(13) & Chr(10)
  308.     strMsg = strMsg & Error
  309.  
  310.     MsgBox strMsg, MB_ICONEXCLAMATION, "Error"
  311.     Exit Sub
  312.  
  313.     Resume Next
  314. End Sub
  315.  
  316. Sub DrawDiagram (NodeList() As NodeInfo)
  317. '----------------------------------------
  318. '--- DrawDiagram ------------------------
  319. '--
  320. '--   Builds the network diagram from the network info table.
  321. '--
  322. '-- Assumptions : InitDiagram succeeded and table is loaded in module
  323. '--               m_tblNetInfo.
  324. '--
  325.     
  326.     On Error GoTo lblBuildDiagramErr
  327.     
  328.     Dim iPinX As Double, iPinY As Double
  329.     Dim iBus As Integer, iMaxBus As Integer
  330.     Dim iHandle As Integer, iNode As Integer
  331.     Dim strMsg As String, strCellName As String
  332.     Dim X, Y, iRow As Integer
  333.     
  334.     Dim shp As Object, mast As Object, bus As Object, cell As Object
  335.     Dim CHandle As VisPoint
  336.  
  337.     ' Next we loop through each node in the list and drop it on the page
  338.     ' according to it's master name.  The drop information and master name
  339.     ' were decided in BuildNodeList().
  340.     '
  341.     m_tblNetInfo.MoveFirst
  342.  
  343.     For iNode = 0 To UBound(NodeList)
  344.         iPinX = NodeList(iNode).iPinX
  345.         iPinY = NodeList(iNode).iPinY
  346.  
  347.         Set mast = m_masts(NodeList(iNode).strMaster)
  348.         Set shp = m_pag.Drop(mast, iPinX, iPinY)
  349.  
  350.         shp.Text = "" & m_tblNetInfo.Fields(1)
  351.         'shp.Data1 = "" & m_tblNetInfo.Fields(2)
  352.         'shp.Data2 = "" & m_tblNetInfo.Fields(3)
  353.  
  354.         m_tblNetInfo.MoveNext
  355.     Next iNode
  356.  
  357.     ' Lastly we draw the ethernet bus and attach it to the nodes.  Notice we
  358.     ' hard code the number of control handles per bus at 5 (iMaxBus).
  359.     '
  360.     Set mast = m_masts(NDB_NET_ETHERNET)        ' Ethernet Bus Master
  361.     
  362.     iNode = 0                                   ' Current Network Node
  363.     iMaxBus = Int((UBound(NodeList) + 1) / 5)   ' Total buses needed
  364.  
  365.     For iBus = 0 To iMaxBus
  366.         ' Becuause the ethernet bus has a limited number of control handles we
  367.         ' stack them on top of each other to make it appear like there is
  368.         ' an unlimited number.  However, we must only use a fill on the bottom
  369.         ' bus to make sure all control lines are seen and only leave the
  370.         ' text on the top bus so it isn't overwritten by the control lines.
  371.         '
  372.         Set bus = m_pag.Drop(mast, 0, 0)        ' Draw Bus
  373.  
  374.         bus.SetBegin (NDB_PAGE_WIDTH / 2), 0
  375.         bus.SetEnd (NDB_PAGE_WIDTH / 2), NDB_PAGE_HEIGHT
  376.         
  377.         If iBus <> 0 Then bus.FillStyle = "None"
  378.         If iBus <> iMaxBus Then bus.Text = ""
  379.  
  380.         For iHandle = 1 To bus.RowCount(visSectionControls)
  381.             iNode = iNode + 1
  382.  
  383.             If iNode <= (UBound(NodeList) + 1) Then
  384.                 ' If we have another node to glue to we simply get the node
  385.                 ' from the page.Shapes collections and glue to the side
  386.                 ' specified in it's NodeInfo using the next available control
  387.                 ' handle.
  388.                 '
  389.                 strCellName = "Controls.X" & LTrim(Str(iHandle))
  390.  
  391.                 Set shp = m_pag.Shapes(iNode)
  392.                 
  393.                 iRow = BestExportPoint(shp, NodeList(iNode - 1).iSide)
  394.  
  395.                 Set cell = shp.CellsSRC(visSectionExport, iRow - 1, 0)
  396.                 
  397.                 bus.Cells(strCellName).GlueTo cell
  398.             Else
  399.                 GetCtrlHandlePt bus, (iHandle), CHandle
  400.  
  401.                 CHandle.X = "LocPinX"
  402.                 CHandle.Y = "LocPinY"
  403.  
  404.                 SetCtrlHandlePt bus, (iHandle), CHandle
  405.             End If
  406.         Next iHandle
  407.     Next iBus
  408.     
  409.     Exit Sub
  410.  
  411. lblBuildDiagramErr:
  412.     strMsg = "Error building network diagram." & Chr(13) & Chr(10) & Error
  413.     MsgBox strMsg, MB_ICONEXCLAMATION, "Error"
  414.     Exit Sub
  415.  
  416.     Resume Next
  417. End Sub
  418.  
  419. Function InitDiagram () As Integer
  420. '----------------------------------------
  421. '--- InitDiagram ------------------------
  422. '--
  423. '--   Initializes diagram by getting the GIO, creating a new drawing and
  424. '-- setting the pag object.  Also sets the page to landscape.
  425. '--
  426.  
  427.     On Error GoTo lblInitDiagramErr
  428.  
  429.     Dim strAction As String
  430.  
  431.     InitDiagram = True
  432.  
  433.     ' First we try and get Visio up and running.
  434.     '
  435.     If vaoGetObject() <> visOK Then
  436.         InitDiagram = False
  437.         MsgBox "Error starting Visio", MB_ICONEXCLAMATION, "Error"
  438.         Exit Function
  439.     End If
  440.  
  441.     ' Next we try and create a document based on the network stencil.  Then
  442.     ' we retrieve the masters from the network stencil.
  443.     '
  444.     strAction = "Opening stencil " & NDB_NET_STENCIL
  445.  
  446.     Set m_pag = g_appVisio.Documents.Add(NDB_NET_TEMPLATE).Pages(1)
  447.     Set m_masts = g_appVisio.Documents(NDB_NET_STENCIL).Masters
  448.     
  449.     ' Next we make the page landscape.
  450.     '
  451.     strAction = "Updating page width and height."
  452.     m_pag.Shapes("thePage").Cells("PageWidth").Formula = NDB_PAGE_WIDTH
  453.     m_pag.Shapes("thePage").Cells("PageHeight").Formula = NDB_PAGE_HEIGHT
  454.  
  455.     Exit Function
  456.  
  457. lblInitDiagramErr:
  458.     MsgBox "Error " & strAction, MB_ICONEXCLAMATION, "Error"
  459.     InitDiagram = False
  460.     Exit Function
  461.  
  462.     Resume Next
  463. End Function
  464.  
  465. Function MatchMasterToType (ByVal strData As String) As String
  466. '----------------------------------------
  467. '--- MatchMasterToType ------------------
  468. '--
  469. '--    Given a node type will return a string containing the closest match
  470. '-- for that type.  If no match can be determined it returns a match to a
  471. '-- PC.
  472. '--
  473.  
  474.     Dim strMatch As String, strType As String
  475.  
  476.     strType = strData
  477.     strMatch = strType
  478.  
  479.     Select Case LCase(strType)
  480.         Case LCase(NDB_NODE_MAC)
  481.         Case LCase(NDB_NODE_PC)
  482.         Case LCase(NDB_NODE_SERVER)
  483.         Case LCase(NDB_NODE_WORKSTATION)
  484.         Case LCase(NDB_NODE_TOWER)
  485.         Case LCase(NDB_NODE_PRINTER)
  486.         Case Else
  487.             strMatch = ""
  488.     End Select
  489.         
  490.     If strMatch = "" Then
  491.         If InStr(1, strType, "tower", 1) Then
  492.             strMatch = NDB_NODE_PC
  493.         ElseIf InStr(1, strType, "apple", 1) Then
  494.             strMatch = NDB_NODE_MAC
  495.         ElseIf InStr(1, strType, "mac", 1) Then
  496.             strMatch = NDB_NODE_MAC
  497.         ElseIf InStr(1, strType, "ibm", 1) Then
  498.             strMatch = NDB_NODE_PC
  499.         ElseIf InStr(1, strType, "pc", 1) Then
  500.             strMatch = NDB_NODE_PC
  501.         ElseIf InStr(1, strType, "clone", 1) Then
  502.             strMatch = NDB_NODE_PC
  503.         ElseIf InStr(strType, "server") Then
  504.             strMatch = NDB_NODE_PC
  505.         ElseIf InStr(1, strType, "file", 1) Then
  506.             strMatch = NDB_NODE_PC
  507.         ElseIf InStr(1, strType, "desktop", 1) Then
  508.             strMatch = NDB_NODE_PC
  509.         Else
  510.             strMatch = NDB_NODE_PC
  511.         End If
  512.     End If
  513.  
  514.     MatchMasterToType = strMatch
  515. End Function
  516.  
  517. Function Max (X, Y)
  518.     If X < Y Then
  519.         Max = Y
  520.     Else
  521.         Max = X
  522.     End If
  523. End Function
  524.  
  525. Function Min (X, Y)
  526.     If X < Y Then
  527.         Min = X
  528.     Else
  529.         Min = Y
  530.     End If
  531. End Function
  532.  
  533. Function ValidDatabase (strFileName As String) As Integer
  534. '----------------------------------------
  535. '--- ValidDatabase ----------------------
  536. '--
  537. '--   Validates a database by opening it and verifying the a valid network
  538. '-- diagramming table exists.
  539. '--
  540.  
  541.     On Error GoTo lblValidDatabaseErr
  542.  
  543.     Dim dbDBase As Database
  544.     Dim NetTable As Table
  545.  
  546.     ValidDatabase = True
  547.  
  548.     ' First we try and open the database.  Access should convert regardless of
  549.     ' format.
  550.     
  551.     Set dbDBase = OpenDatabase(strFileName)
  552.  
  553.     ' No error occurred so we try and open our table.  We use a constant
  554.     ' name for a database at this point but should prompt the user in the
  555.     ' future for what table they want to use.
  556.     
  557.     Set NetTable = dbDBase.OpenTable(NDB_NET_TABLE_NAME)
  558.  
  559.     ' If Nothing is returned the table doesn't exist and we consider the
  560.     ' database invalid.  If a table is returned we check to make sure it
  561.     ' contains 4 fields:
  562.     '
  563.     ' Network Info Table Structure
  564.     '   +-----------+-----------+-------------+-------------+
  565.     '   | Node Type | Node Text | Node Data 1 | Node Data 2 |
  566.     '   +-----------+-----------+-------------+-------------+
  567.     
  568.     If (NetTable Is Nothing) Or (NetTable.Fields.Count <> 4) Then
  569.         ValidDatabase = False
  570.     End If
  571.  
  572.     ' Cleanup by closing table and database.
  573.     
  574.     NetTable.Close
  575.     dbDBase.Close
  576.  
  577.     Exit Function
  578.  
  579. lblValidDatabaseErr:
  580.     ValidDatabase = False
  581.     Exit Function
  582.  
  583.     Resume Next
  584. End Function
  585.  
  586.